import { useDictOptions } from '@/utils/dict'
import type { ButtonType } from '../types'

export interface ButtonStyle {
  tagType: string
  bgColor: string
  textColor: string
  borderColor: string
}

export function useButtonConfig() {
  // 从字典获取按钮类型选项
  const { options: buttonTypeOptions, ready } = useDictOptions('task_operation')

  // 获取按钮类型的标签
  const getButtonTypeLabel = (type: ButtonType) => {
    const option = buttonTypeOptions.value.find((opt) => opt.value === type)
    return option?.label || ''
  }

  // 获取按钮样式配置
  const getButtonStyle = (type: ButtonType): ButtonStyle => {
    const option = buttonTypeOptions.value.find((opt) => opt.value === type)
    if (!option?.config) {
      return {
        tagType: 'info',
        bgColor: 'var(--el-color-info-light-9)',
        textColor: 'var(--el-color-info)',
        borderColor: 'var(--el-color-info-light-5)',
      }
    }
    try {
      return JSON.parse(option.config)
    } catch {
      return {
        tagType: 'info',
        bgColor: 'var(--el-color-info-light-9)',
        textColor: 'var(--el-color-info)',
        borderColor: 'var(--el-color-info-light-5)',
      }
    }
  }

  return {
    buttonTypeOptions,
    ready,
    getButtonTypeLabel,
    getButtonStyle,
  }
}
